-----------------------------------------------------------------------------------------
-- filename : messagebox_scene.lua
-- description : Simple message box, showing text and yes/no button
-----------------------------------------------------------------------------------------

-- Load needed libraries/scripts
local callback = nil
local storyboard = require "storyboard"
local scene = storyboard.newScene()
local layer = require "layer"

function scene:createScene(event)
	print("overlay")
	callback = event.params.callback
	local group = self.view
	
	local back = display.newRect( group, display.screenOriginX, display.screenOriginY, display.actualContentWidth, display.actualContentHeight )
	back:setFillColor(0, 0, 0, 1.0)
	back:addEventListener("touch", castRay)
	back:addEventListener("tap", castRay)

	local background = display.newImageRect( "assets/confirmation/background.png",480,200)
	background.x = 480/2
	background.y = 320/2

	-- Button
	local yesImage = display.newImageRect( "assets/confirmation/yes.png", 200, 100 )
	yesImage.x = 480/2 -100
	yesImage.y = 320/2
	yesImage:addEventListener("touch", yes)

	local noImage = display.newImageRect( "assets/confirmation/no.png", 200, 100 )
	noImage.x = 480/2 + 100
	noImage.y = 320/2
	noImage:addEventListener("touch", no)

	local textConf = display.newText(event.params.text or "", 480/2, 320/2-100, native.systemFontBold, 24)
	textConf:setTextColor( 0, 0, 0 )
	textConf.x = 480/2

	group:insert(background)
	group:insert(yesImage)
	group:insert(noImage)
	group:insert(textConf)
end

function castRay( event )
	print("Hello Cast")
	return true
end

function yes( event )
	if(event.phase == "began") then
		print("yes")
		if(callback ~= nil) then
			local newEvent = {
				result = 1
			}
			storyboard.hideOverlay()
			callback:callbackListen(newEvent)
		end
		
	end
	return true
end

function no( event )
	if(event.phase == "began") then
		print("yes")
		if(callback ~= nil) then
			local newEvent = {
				result = 0
			}
			callback:callbackListen(newEvent)
		end
		storyboard.hideOverlay("fade",800)
	end
	return true
end

-- add event listener
scene:addEventListener("createScene", scene)

return scene